//
//  TKIMPhotoSelectHelper.m
//  TKPluginDemo
//
//  Created by liupm on 16/1/12.
//  Copyright © 2016年 liupm. All rights reserved.
//

#import "TKIMPhotoSelectHelper.h"


@interface TKIMPhotoSelectHelper()
//相册库
@property(nonatomic,strong) ALAssetsLibrary *library;

@end


@implementation TKIMPhotoSelectHelper


/**
 *  @Author lpm, 16-01-12 10:01:12
 *
 *  @brief  获得相册组
 *
 *  @return 返回的数据中的子元素为TKIMAlbumModel
 */
-(void)getAlbumGroups:(TKIMImagePickerCallBack)callBack
{
    NSMutableArray *groups = [NSMutableArray array];
    [self.library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
     {
         if(group)
         {
             [group setAssetsFilter:[ALAssetsFilter allPhotos]];
             TKIMAlbumModel *album = [[TKIMAlbumModel alloc] init];
             album.albumGroup = group;
             album.albumName =  [group valueForProperty:@"ALAssetsGroupPropertyName"];
             album.thumbImage = [UIImage imageWithCGImage:group.posterImage];
             album.photoCount = group.numberOfAssets;
             if (album.photoCount)
             {
                 [groups addObject:album];
             }
         }
         else
         {
             callBack(groups);
         }
     } failureBlock:^(NSError *error)
     {
         callBack(nil);
     }];
}


/**
 *  @Author lpm, 16-01-12 10:01:43
 *
 *  @brief  获得手机中的所有的图
 *  ALAsset类有一个defaultRepresentation方法，返回值是ALAssetRepresentation类，该类的作用就是获取该资源图片的详细资源信息。
     //获取资源图片的详细资源信息
     ALAssetRepresentation* representation = [asset defaultRepresentation];
     //获取资源图片的长宽
     CGSize dimension = [representation dimensions];
     //获取资源图片的高清图
     [representation fullResolutionImage];
     //获取资源图片的全屏图
     [representation fullScreenImage];
     //获取资源图片的名字
     NSString* filename = [representation filename];
     NSLog(@"filename:%@",filename);
     //缩放倍数
     [representation scale];
     //图片资源容量大小
     [representation size];
     //图片资源原数据
     [representation metadata];
     //旋转方向
     [representation orientation];
     //资源图片url地址，该地址和ALAsset通过ALAssetPropertyAssetURL获取的url地址是一样的
     NSURL* url = [representation url];
     NSLog(@"url:%@",url);
     //资源图片uti，唯一标示符
     NSLog(@"uti:%@",[representation UTI]);
 *  @return  返回的数据中的子元素为TKIMPhotoModel
 */
-(void)getAllPhotos:(TKIMAlbumModel *)album  callBack:(TKIMImagePickerCallBack)callBack
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSMutableArray *photos = [NSMutableArray array];
        [album.albumGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
            if (result)
            {
                TKIMPhotoModel *photo = [[TKIMPhotoModel alloc] init];
                photo.asset = result;
                //获得原图会消耗大量资源
                //photo.originImage = [UIImage imageWithCGImage: result.defaultRepresentation.fullScreenImage];
                //photo.thumbImage = [UIImage imageWithCGImage:result.aspectRatioThumbnail];
                [photos addObject:photo];
            }
        }];
        dispatch_async(dispatch_get_main_queue(), ^{
            callBack(photos);
        });
    });
}



#pragma mark  私有属性

-(ALAssetsLibrary *)library
{
    if (!_library)
    {
        _library = [[ALAssetsLibrary alloc] init];
    }
    return _library;
}

@end
